package org.xb;

/**
 * @Author
 * @Description 懒汉式
 * @Date created in 10:05 2024/10/22
 */
public class Main05 {
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                Person5 instance = Person5.getInstance();
                System.out.println(instance.hashCode());
            }).start();
        }
    }
}

class Person5 {
    private Person5() {
    }
    //DCL的时候是需要加volatile的
    private volatile static Person5 person;

    public static Person5 getInstance() {
        if (person == null) {

            synchronized (Person5.class) {
                if (person == null) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    person = new Person5();
                }
            }
        }
        return person;
    }
}


